﻿using System;
using Nofs.Net.Common.Interfaces.Library;
using biz.ritter.javapi.util;

namespace Nofs.Net.nofs.netfs
{

    //@RootFolderObject
    //@DomainObject
    //@FolderObject(CanAdd=false, CanRemove=false)
    public class Network
    {
        private IDomainObjectContainerManager _containerManager;

        //@NeedsContainerManager
        public void setContainerManager(IDomainObjectContainerManager manager)
        {
            _containerManager = manager;
        }

        //@SuppressWarnings("unchecked")
        //@FolderObject(CanAdd=false, CanRemove=false)
        public LinkedList<TCPConnection> gettcp() //throws Exception 
        {
            LinkedList<TCPConnection> connections = new LinkedList<TCPConnection>();
            connections.addAll((Collection<TCPConnection>)
                    _containerManager.GetContainer(typeof(TCPConnection).getClass()).GetAllInstances());
            return connections;
        }

        //@SuppressWarnings("unchecked")
        //@Executable
        public void CreateConnection(String type, String name)// throws Exception 
        {
            Console.WriteLine("CreateConnection(" + type + "," + name + ")");
            if (type.ToUpperInvariant().compareTo("TCP") == 0)
            {
                IDomainObjectContainer<TCPConnection> tcpContainer = _containerManager.GetContainer(typeof(TCPConnection).getClass()) as IDomainObjectContainer<TCPConnection>;
                TCPConnection connection = tcpContainer.NewPersistentInstance();
                connection.setName(name);
                tcpContainer.ObjectChanged(connection);
                _containerManager.GetContainer(typeof(Network).getClass()).ObjectChanged(this);
                Console.WriteLine("Done, success");
                //return 0;
            }
            //return 1;
        }

        //@SuppressWarnings("unchecked")
        //@Executable
        public void RemoveConnection(String type, String name) //throws Exception 
        {
            if (type.ToUpperInvariant().compareTo("TCP") == 0)
            {
                IDomainObjectContainer<TCPConnection> tcpContainer = (IDomainObjectContainer<TCPConnection>)_containerManager.GetContainer(typeof(TCPConnection).getClass());
                IDomainObjectContainer<ErrorLog> logContainer = (IDomainObjectContainer<ErrorLog>)_containerManager.GetContainer(typeof(ErrorLog).getClass());
                foreach (TCPConnection connection in tcpContainer.GetAllInstances())
                {
                    if (connection.getName().compareTo(name) == 0)
                    {
                        tcpContainer.Remove(connection);
                        logContainer.Remove(connection.getLog().get(0));
                        _containerManager.GetContainer(typeof(Network).getClass()).ObjectChanged(this);
                        break;
                    }
                }
            }
        }
    }
}
